const express = require('express')
const router = express.Router()
const db = require('../../connection/Connection')
const jwt = require('jsonwebtoken')
const SECRET = 'asdfta219*&%^jWJSHFJ'


router.post('/addAttendanceInfo', async (req, res) => {
    //token
    const result = jwt.verify(req.body.token, SECRET)
    let sql = 'select departmentId from user_role where username = ?'
    db.base(sql, result.username, response => {
        //验证成功
        if (response.length == 1) {
            console.log(req.body)
            sql = 'SELECT attendance_id from attendance_department WHERE workNumber = ? and  departmentId = ?'
            db.base(sql, [req.body.addAttendanceInfo.workNumber, response[0].departmentId], response => {
                if (response.length == 1) {
                    sql = 'INSERT INTO attendance_id (attendance_id,matter,occur_time) VALUES(?,?,?)'
                    let data = [response[0].attendance_id, req.body.addAttendanceInfo.matter, req.body.addAttendanceInfo.occur_time]
                    db.base(sql, data, response => {
                        console.log(response)
                        if (response!= null && response.affectedRows == 1) {
                            res.json({
                                result:"success"
                            })
                        } else {
                            res.json({
                                result:"error"
                            })
                        }
                    })
                }
            })
        }
    })

})

module.exports = router
